#!/usr/bin/perl
# Script to scan an output file from a solver and print summary stats. This
# solvers print summary stats when they are done running, but this is handy
# while the solver is running.
$n= @ARGV;
for ($i= 0; $i < $n; $i++)
{
    $file= $ARGV[$i];
    open FILE, $file or die "Cannot Open File '$file'\n";

    my $nplay= 0;
    my $nwin= 0;
    my $nstuck= 0;
    my $nabandon= 0;
    my $noff= 0;
    while ($line= <FILE>)
    {
	if ($line =~ /^maxoff=(\d+)/)
	{
	    $noff+= $1;
	}
	elsif ($line =~ /^Victory/)
	{
	    $nplay++;
	    $nwin++;
	}
	elsif ($line =~ /^Stuck/)
	{
	    $nplay++;
	    $nstuck++;
	}
	elsif ($line =~ /^Abandoned!/)
	{
	    $nplay++;
	    $nabandon++;
	}
    }

    print "\n" if $i > 0;
    print qq($file\n) if $n > 1;
    $nlost= $nplay - $nwin - $nabandon;
    print qq(Played:    $nplay games
Won:        $nwin games ),
	$nplay > 0 ? sprintf("(%.2f\%)",$nwin*100/$nplay) : '',qq(
Unwinnable: $nlost games ),
	$nplay > 0 ? sprintf("(%.2f\%)",$nlost*100/$nplay) : '',qq(
Abandoned:  $nabandon games ),
	$nplay > 0 ? sprintf("(%.2f\%)",$nabandon*100/$nplay) : '',qq(
Cards Off:  ),
	$nplay > 0 ? sprintf("%.2f",$noff/$nplay) : '',qq(\n);
}
